import tkinter as tk


# 鼠标按下
def on_mouse_down(event):
    # 当鼠标按下时，开始绘图
    canvas.old_x = event.x
    canvas.old_y = event.y


# 鼠标抬起
def on_mouse_up(event):
    # 颜色值转换为十六进制颜色值
    hex_color = "#{:02x}{:02x}{:02x}".format(color_value, color_value, color_value)
    # 当鼠标移动时，根据鼠标位置绘制线条
    if canvas.old_x and canvas.old_y:
        if abs(event.x - canvas.old_x) > abs(event.y - canvas.old_y):
            canvas.create_line(canvas.old_x, canvas.old_y, event.x, canvas.old_y, width=pen_size, fill=hex_color)
        else:
            canvas.create_line(canvas.old_x, canvas.old_y, canvas.old_x, event.y, width=pen_size,fill=hex_color)
    
    # 当鼠标释放时，停止绘图
    canvas.old_x = None
    canvas.old_y = None

# 清空画布，默认为白色
def clear_canvas():
    canvas.delete("all")
    canvas.create_rectangle(0, 0, width, height, fill="white")

def map_start(self):
    # 重绘功能，这里暂时不实现具体绘制逻辑
    pass

def map_w(value):
    try:
        width = int(value)
        canvas.config(width=width)
    except ValueError:
        pass

def map_h(value):
    try:
        height = int(value)
        canvas.config(height=height)
    except ValueError:
        pass

def map_color(value):
    color_value = int(value)
    # print(color_value,value)
    # color_value = int(value)

def map_line(value):
    
    try:
        pen_size = int(value)
    except ValueError:
        pass

def show_map(self):
    # 这里需要实现具体的地图绘制逻辑，暂时不实现
    pass

def move_y(n):
    box_y = box_y + n
    show_map()

def move_x(n):
    box_x = box_x + n
    show_map()

def show_w(n):
    try:
        box_w = int(n)
        show_canvas.config(width=box_w)
    except ValueError:
        pass

def show_h(n):
    try:
        box_h = int(n)
        show_canvas.config(height=box_h)
    except ValueError:
        pass


root = tk.Tk()
root.title("栅格地图（像素法）")

# 初始化变量
width = 300
height = 300
color_value = 50
pen_size = 10
box_x = 0
box_y = 0
box_w = 100
box_h = 100

f1 = tk.Frame(root)
f1.pack()
# 重绘按钮
tk.Button(f1, text="重绘", command=map_start).pack(side=tk.LEFT)
# 宽度输入框
tk.Label(f1, text="宽度").pack(side=tk.LEFT)
width_entry = tk.Entry(f1, width=3)
width_entry.insert(0, str(width))
width_entry.bind("<Return>", lambda event: map_w(width_entry.get()))
width_entry.pack(side=tk.LEFT)
# 高度输入框
tk.Label(f1, text="高度").pack(side=tk.LEFT)
height_entry = tk.Entry(f1, width=3)
height_entry.insert(0, str(height))
height_entry.bind("<Return>", lambda event: map_h(height_entry.get()))
height_entry.pack(side=tk.LEFT)

# 颜色输入框
tk.Label(f1, text="黑色←").pack(side=tk.LEFT)
color_entry = tk.Entry(f1, width=3)
color_entry.insert(0, str(color_value))
color_entry.bind("<FocusOut>", lambda event: map_color(color_entry.get()))
color_entry.pack(side=tk.LEFT)
tk.Label(f1, text="→白色").pack(side=tk.LEFT)

# 画笔大小输入框
tk.Label(f1, text="画笔").pack(side=tk.LEFT)
pen_entry = tk.Entry(f1, width=2)
pen_entry.insert(0, str(pen_size))
pen_entry.bind("<FocusOut>", lambda event: map_line(pen_entry.get()))
pen_entry.pack(side=tk.LEFT)

# 创建画布
canvas = tk.Canvas(root, width=width, height=height, borderwidth=1, relief="solid")
canvas.pack()
# 绑定鼠标事件
canvas.bind("<Button-1>", on_mouse_down)
canvas.bind("<ButtonRelease-1>", on_mouse_up)


f2 = tk.Frame(root)
f2.pack()
# 宽高输入框（俯视图）
tk.Label(f2, text="宽").pack(side=tk.LEFT)
show_width_entry = tk.Entry(f2, width=3)
show_width_entry.insert(0, str(box_w))
show_width_entry.bind("<Return>", lambda event: show_w(show_width_entry.get()))
show_width_entry.pack(side=tk.LEFT)

tk.Label(f2, text="高").pack(side=tk.LEFT)
show_height_entry = tk.Entry(f2, width=3)
show_height_entry.insert(0, str(box_h))
show_height_entry.bind("<Return>", lambda event: show_h(show_height_entry.get()))
show_height_entry.pack(side=tk.LEFT)

tk.Button(f2, text="显示俯视图", command=lambda: move_y(0)).pack(side=tk.LEFT)

# 创建俯视图画布
show_canvas = tk.Canvas(root, width=box_w, height=box_h,borderwidth=1, relief="solid")
show_canvas.pack()
f3 = tk.Frame(root)
f3.pack()
# 上下左右移动按钮
tk.Button(f3, text="上", command=lambda: move_y(-2)).pack(side=tk.LEFT)
tk.Button(f3, text="下", command=lambda: move_y(2)).pack(side=tk.LEFT)
tk.Button(f3, text="左", command=lambda: move_x(-2)).pack(side=tk.LEFT)
tk.Button(f3, text="右", command=lambda: move_x(2)).pack(side=tk.LEFT)
root.mainloop()